/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */


webfs_vfsNode = vfsList[vfsList.length]=new vfsNode();
webfs_vfsNode.Name = 'webfs';

function webNode() {
    fileNode.Name = "";
    fileNode.meta = 0;
    fileNode.next = 0;
    fileNode.prev = 0;
    fileNode.Data = 0;
    fileNode.Parent = 0;
};

var winfs;

if (!this.webfs) webfs = {};

webfs_vfsNode.item=webfs;

webfs.version = '0.1';

webfs.update = function(event)
{
    alert('!');
    
};

webfs.init = function()
{
    winfs = new webNode();
    winfs.Name="/";
    winfs.Type=1;
    winfs.prev=0;
    winfs.next=0;
 };

webfs.mount =  function(serverAddress,fs_type,mountPoint,user,pass,argv)
{
    webfs.init();
    return 0;
};
    
webfs.umount = function()
{
    winfs = 0;
    return 0;
};

webfs.read = function(path,flags,buffer,offset,length,sessionKey)
{
    if(flags =='1')
    {
        var readNode = webfs.walk(path);

        if(readNode.Type == 1)
            return 1;
        if(readNode.Type==5)
        {
            readNode.Data[3].read(path,flags,buffer,offset,length,sessionKey);
            return -1;
        }           
        else
            return 0;    
    }
    
    if(flags == '2')
    {
       var readNode = webfs.walk(path);

        if(readNode.Type == 1)
            return readNode;
        if(readNode.Type==5)
        {
            return(readNode.Data[3].read(readNode.Name,flags,buffer,offset,length,sessionKey) );
        }           
        else
            return readNode;       
    }
    if(flags =='3')
    {  
        var readNode = webfs.walk(path);
        benix.stdout(readNode.Type + '<br>');
        
        if(readNode.Type == 1)
        {
            if(readNode.Data[0].Name)
            return readNode.Data[0].Name;
            else
            return "File Read Error"
        }
        if(readNode.Type==5)
        {
            alert('recursive mounts not support yet');
            return(0);//readNode.Data[3].read(readNode.Name,flags) );
        }           
        else
            return readNode.Data;    
    }
    else 
        return -1;
};

webfs.write = function(path,flags,buffer,offset,length,sessionKey)
{
/*
    FS File Read Flags
    1 Request Type
    2 Return Array of File in Directory
    3 Read text/data file*/
    var workPath = '/';
    var aNode = 0;
    var pathArray = path.split('/');
    var counter=0;

    while(counter <= pathArray.length-2)
    {
        if(workPath == '/')
            workPath = workPath + pathArray[counter];
        else
            workPath = workPath + '/' + pathArray[counter];
        counter++;
    }

    if(flags==0)
    {
        //unlink a node
            counter = 0;
            aNode = webfs.walk(path);
            
            webfs.removewebfs(aNode.Name);
            
            var prevNode = aNode.prev;   
            var nextNode = aNode.next;      

            while(aNode.Parent.Data[counter].Name != aNode.Name && counter < aNode.Parent.Data.length)
            {
                counter++;
            }
            
            aNode.Parent.Data.splice(counter,1);
            
            prevNode.next = nextNode;
            nextNode.prev = prevNode;
    }
    
    if(flags==1)
    {
        aNode = webfs.walk(workPath);

        if(aNode.Type == 1 && buffer == 1)
        {
            aNode.Data[aNode.Data.length] = new fileNode;
            var newDir = aNode.Data[aNode.Data.length-1];
            newDir.Name = pathArray[pathArray.length-1];
            newDir.Type = 1;
            newDir.next = 0;
            newDir.Data = new Array();
            newDir.prev = aNode.Data[aNode.Data.length-1];
            newDir.Parent = aNode;

            if(aNode.Data.length >= 2 )
            {        
                aNode.Data[aNode.Data.length-2].next=newDir;
            }  
            
            webfs.makewebfs(pathArray[pathArray.length-1],null);
        }
        if(aNode.Type == 1 && buffer == 2)
        {
            var newfile = aNode.Data[aNode.Data.length] = new fileNode;
            newfile.Name = pathArray[pathArray.length-1];
            newfile.Type = 2;
            newfile.next = 0;
            newfile.Data = buffer;
            newfile.prev = aNode.Data[aNode.Data.length-1];
            aNode.Data[aNode.Data.length-1].next=newFile;
        }
        
        if(aNode.Type==5 )
        {
            var loadPath = benix.fsBuildMountPath(aNode,path);

            return(aNode.Data[3].write(loadPath,flags,buffer) );
        }  
        
        else
            return -1;
    }
    else if(flags==2)
    {
        aNode = webfs.walk(workPath);

        if(aNode.Type==1 )
        {
            var newfile = aNode.Data[aNode.Data.length] = new fileNode();
            newfile.Name = pathArray[pathArray.length-1];
            newfile.Type = 2;
            newfile.next = 0;
            newfile.Data = buffer;
            newfile.prev = aNode.Data[aNode.Data.length-1];
            aNode.Data[aNode.Data.length-2].next=newfile;  
            
            webfs.addwebfsStyle(aNode.Name,newfile.Name,newfile.buffer);
        }
        
        if(aNode.Type==5 )
        {
            var loadPath = benix.fsBuildMountPath(aNode,path);

            return(aNode.Data[3].write(loadPath,flags,buffer,offset,length,sessionKey) );
        }  
        
        else
            return -1;
    }
    
    else if(flags==3)
    {
    
    }
    else 
        return -1;
};

webfs.walk = function(fsNode)
{
 return(benix.fsWalk(winfs,fsNode) );
};
